<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Document</title>
  <link rel="stylesheet" href="./css/bootstrap.min.css" />
  <style>
    .form-select {
      width: 103px;
      display: inline-block;
    }

    .col-form-label {
      text-align: right;
    }

    .figure-img {
      width: 100px;
      height: 100px;
      cursor: pointer;
    }

    #upload {
      display: none;
    }
  </style>
</head>

<body>
  <div class="container">
    <h1 class="p-5">个人设置</h1>
    <form class="col-4">
      <div class="row mb-3">
        <label class="col-form-label col-3">昵称：</label>
        <div class="col-9">
          <input class="form-control col-9" type="text" name="nickname" />
        </div>
      </div>
      <div class="row mb-3">
        <label class="col-form-label col-3">籍贯：</label>
        <div class="col-9">
          <select class="form-select col-4" name="province">
            <option value="">--省--</option>
          </select>
          <select class="form-select col-4" name="city">
            <option value="">--市--</option>
          </select>
          <select class="form-select col-4" name="area">
            <option value="">--区--</option>
          </select>
        </div>
      </div>
      <div class="row mb-3">
        <label class="col-form-label col-3">头像：</label>
        <div class="col-9">
          <input class="form-control col-9" type="hidden" name="avatar" />
          <figure class="figure">
            <input type="file" id="upload" />
            <img src="https://yanxuan-item.nosdn.127.net/12a882699bd531a1bd428bffe1989525.jpg"
              class="figure-img img-fluid rounded" alt="..." />
            <figcaption class="figure-caption">修改头像</figcaption>
          </figure>
        </div>
      </div>
      <div class="row mb-3">
        <label class="col-3"></label>
        <div class="col-9">
          <button class="btn btn-primary">保存</button>
        </div>
      </div>
    </form>
  </div>

  <script src="./lib/bootstrap.min.js"></script>
  <script src="./lib/axios.js"></script>
  <script>
    // 上传头像
    const upload = document.querySelector('#upload')
    const avatar = document.querySelector('.figure img')
    const btnPrimary = document.querySelector('.btn-primary')
    // 昵称
    const nickname = document.querySelector('[name=nickname]')

    // 省市区
    const province = document.querySelector('[name=province]')
    const city = document.querySelector('[name=city]')
    const area = document.querySelector('[name=area]')


    // 接口文档：https://www.showdoc.com.cn/1834761734600444/8477923164668939


    // 功能列表
    // 功能1: 上传头像
    // 功能2: 页面初始化: 获取个人信息，并渲染到页面中
    // 功能3: 省市区的联动效果
    // 功能4: 保存个人信息，刷新页面，可以看到自己保存的个人信息
    //        说明：由于接口提供的是相同账号下的个人信息，所以当大家都在练习的时候，会出现互相覆盖的情况


    // params: {}
    // 要求： 
    // 1. 使用 axios 发送请求
    // 2. 设置 axios 请求根路径
    // 3. 配合 async await 完成功能
    // 配置根路径
    axios.defaults.baseURL = 'http://ajax-api.itheima.net'
    // axios.defaults.baseURL = 'http://124.223.14.236:3001/api'

    // 封装省
    async function fn1() {
      const { data: { data: res1 } } = await axios.get('/api/province')
      // console.log(res1);
      const provinceStr = res1.map(item => `<option value="${item}">${item}</option>`)
      //渲染
      province.innerHTML = ' <option value="">--省--</option>' + provinceStr
    }
    // 封装市
    async function fn2(province) {
      const { data: { data: res2 } } = await axios.get('/api/city', {
        params: { pname: province }
      })
      // console.log(res2);
      const cityStr = res2.map(item => `<option value="${item}">${item}</option>`)
      city.innerHTML = ' <option value="">--市--</option>' + cityStr
    }
    // 封装区
    async function fn3(province, city) {
      const { data: { data: res3 } } = await axios.get('/api/area', {
        params: {
          pname: province,
          cname: city
        }
      })
      // console.log(res3);
      const areaStr = res3.map(item => `<option value="${item}">${item}</option>`)
      area.innerHTML = ' <option value="">--区--</option>' + areaStr
    }
    async function init() {
      //获取个人信息
      const { data: { data: res } } = await axios.get('/api/settings')
      // console.log(res);
      //显示获取的昵称
      nickname.value = res.nickname
      // 显示获取的头像
      avatar.src = res.avatar

      //获取省
      // const { data: { data: res1 } } = await axios.get('/api/province')
      // // console.log(res1);
      // const provinceStr = res1.map(item => `<option value="${item}">${item}</option>`)
      // province.innerHTML = ' <option value="">--省--</option>' + provinceStr
      await fn1()
      //默认
      province.value = res.province

      //获取市
      // const { data: { data: res2 } } = await axios.get('/api/city', {
      //   params: { pname: res.province }
      // })
      // // console.log(res2);
      // const cityStr = res2.map(item => `<option value="${item}">${item}</option>`)
      // city.innerHTML = ' <option value="">--市--</option>' + cityStr
      await fn2(res.province)
      //默认
      city.value = res.city

      //获取区
      // const { data: { data: res3 } } = await axios.get('/api/area', {
      //   params: {
      //     pname: res.province,
      //     cname: res.city
      //   }
      // })
      // // console.log(res3);
      // const areaStr = res3.map(item => `<option value="${item}">${item}</option>`)
      // area.innerHTML = ' <option value="">--区--</option>' + areaStr
      await fn3(res.province, res.city)
      //默认
      area.value = res.area
    }
    init()

    // change事件触发市区
    province.addEventListener('change', async () => {
      //获取市
      // const { data: { data: res2 } } = await axios.get('/api/city', {
      //   params: { pname: province.value }
      // })
      // // console.log(res2);
      // const cityStr = res2.map(item => `<option value="${item}">${item}</option>`)
      // city.innerHTML = ' <option value="">--市--</option>' + cityStr
      fn2(province.value)
      // bug
      area.innerHTML = ' <option value="">--区--</option>'
    })

    city.addEventListener('change', async () => {
      //获取区
      // const { data: { data: res3 } } = await axios.get('/api/area', {
      //   params: {
      //     pname: province.value,
      //     cname: city.value
      //   }
      // })
      // // console.log(res3);
      // const areaStr = res3.map(item => `<option value="${item}">${item}</option>`)
      // area.innerHTML = ' <option value="">--市--</option>' + areaStr
      fn3(province.value, city.value)
    })



    let quan
    // 上传头像修改图片显示change事件
    avatar.addEventListener('click', () => upload.click())
    upload.addEventListener('change', async () => {
      // console.log(upload.files);
      const file = upload.files[0]
      if (!file) return
      const fd = new FormData()
      fd.append('avatar', file)
      const { data: { data: { url } } } = await axios.post('/api/file', fd)
      // console.log(uploadSrc);
      //显示头像
      avatar.src = url
      quan = url

    })

    //保存信息
    tou(quan)
    function tou(avatar) {
      btnPrimary.addEventListener('click', e => {
        e.preventDefault()
        axios({
          methed: 'put',
          url: '/api/settings',
          data: {
            nickname: nickname.value,
            province: province.value,
            city: city.value,
            area: area.value,
            avatar
          }
        }).then(function (item) {
          console.log(item);
        })
      })
    }




  </script>
</body>

</html>